﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Web;
using ZhangBY.Common;

namespace ZhangBY.Config
{
    public class CompanyBaseConfigFileManager:DefaultConfigFileManager
    {
        private  CompanyBaseConfigInfo m_configinfo;

        /// <summary>
        /// 锁对象
        /// </summary>
        private static object m_lockHelper = new object();

        /// <summary>
        /// 文件修改时间
        /// </summary>
        private  DateTime m_fileoldchange;     


        /// <summary>
        /// 初始化文件修改时间和对象实例
        /// </summary>
        public CompanyBaseConfigFileManager(string filename,string userFolder)
        {
            FileName = filename;
            UserFolder = userFolder;
            m_fileoldchange = System.IO.File.GetLastWriteTime(ConfigFilePath);
            m_configinfo = (CompanyBaseConfigInfo)DefaultConfigFileManager.DeserializeInfo(ConfigFilePath, typeof(CompanyBaseConfigInfo));
        }

        /// <summary>
        /// 当前配置类的实例
        /// </summary>
        public new  IConfigInfo ConfigInfo
        {
            get { return m_configinfo; }
            set { m_configinfo = (CompanyBaseConfigInfo)value; }
        }
        private string userFolder = null;
        /// <summary>
        /// 用户文件夹
        /// </summary>
        public string UserFolder
        {
            get { return userFolder; }
            set { userFolder = value; }
        }
        private string filename = null;
        /// <summary>
        /// 配置文件所在路径
        /// </summary>
        public string FileName
        {
            get { return filename; }
            set { filename = value; }
        }
        private string filepath = null;
        /// <summary>
        /// 获取配置文件所在路径
        /// </summary>
        public new  string ConfigFilePath
        {
            get
            {
                if (filepath == null)
                {
                    HttpContext context = HttpContext.Current;
                    if (context != null)
                    {
                        filepath = context.Server.MapPath(string.Format("~/aspx/company/userfolder/{0}/{1}",UserFolder,FileName));
                        if (!File.Exists(filename))
                        {
                            filepath = context.Server.MapPath(string.Format("~/aspx/company/userfolder/{0}/{1}", UserFolder, FileName));
                        }
                    }
                    else
                    {
                        filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,FileName);
                    }

                    if (!File.Exists(filepath))
                    {
                        throw new ZBYException(string.Format("发生错误: 虚拟目录或网站根目录下没有正确的{0}文件",FileName));
                    }
                }
                return filepath;
            }

        }

        /// <summary>
        /// 加载配置类
        /// </summary>
        /// <returns></returns>
        public CompanyBaseConfigInfo LoadConfig()
        {
            ConfigInfo = DefaultConfigFileManager.LoadConfig(ref m_fileoldchange, ConfigFilePath, ConfigInfo);
            return ConfigInfo as CompanyBaseConfigInfo;
        }

        /// <summary>
        /// 加载真正有效的配置类
        /// </summary>
        /// <returns></returns>
        public CompanyBaseConfigInfo LoadRealConfig()
        {
            ConfigInfo = DefaultConfigFileManager.LoadConfig(ref m_fileoldchange, ConfigFilePath, ConfigInfo, false);
            return ConfigInfo as CompanyBaseConfigInfo;
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <returns></returns>
        public override bool SaveConfig()
        {
            return base.SaveConfig(ConfigFilePath, ConfigInfo);
        }
    }
}
